package com.example.fx.fg

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.fx.R
import com.example.fx.adapter.GoodsSearchAdapter
import com.example.fx.adapter.OneAdapter
import com.example.fx.adapter.TwoAdapter
import com.example.fx.databinding.FragmentCateBinding
import com.example.fx.fgViewModel.CateViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CateFragment : BaseFragment<CateViewModel,FragmentCateBinding>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsSearchAdapter: GoodsSearchAdapter

    var page = 1//当前页码为1
    var cid = 534//二级分类的第一个的id
    override fun getLayout(): Int  = R.layout.fragment_cate

    override fun getViewModel(): Class<CateViewModel>  = CateViewModel::class.java

    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,3)

        goodsSearchAdapter = GoodsSearchAdapter()
        binding.rvGoods.adapter = goodsSearchAdapter
        binding.rvGoods.layoutManager = LinearLayoutManager(context)
        //请求一二级分类数据
        viewModel.category()
        //点击一级分类，二级分类数据变化  三级分类数据变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            page = 1//当分类发生变化时页面要恢复成1
            oneAdapter.setId(oneAdapter.data[position].id)//一级分类默认选中发生修改
            //先将之前的二级分类数据清空
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            cid = twoAdapter.data[0].id
            twoAdapter.setId(cid)//二级分类默认的id也发生了变化
            //三级数据变化
            viewModel.three(cid,1,2)
        }
        //点击二级分类，三级数据发生变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            page = 1//当分类发生变化时页面要恢复成1
            cid = twoAdapter.data[position].id
            twoAdapter.setId(cid)//默认选中发生变化
            viewModel.three(cid,1,2)
        }
        //刷新加载更多
        binding.srl.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.three(cid,page,2)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.three(cid,page,2)
                binding.srl.finishLoadMore()
            }
        })
        //点击三级分类，跳转到详情页面
        goodsSearchAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity")
                .withInt("goodsId",goodsSearchAdapter.data[position].id).navigation()
        }

    }

    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //加载一级分类数据
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //加载二级分类
                twoAdapter.data.clear()
                twoAdapter.data.addAll(it.data[0].child)//获取一级分类的第一个的child
                twoAdapter.notifyDataSetChanged()
                //加载三级分类
                cid = twoAdapter.data[0].id
                viewModel.three(cid,1,2)
            }
        }
        viewModel.threeSuccess.observe(this){
            if(it.code==200){
                /*if(it.data.list.size>0){
                    goodsSearchAdapter.data.clear()
                    goodsSearchAdapter.data.addAll(it.data.list)
                    goodsSearchAdapter.notifyDataSetChanged()
                }*/
                if(page==1){
                    goodsSearchAdapter.data.clear()
                }
                goodsSearchAdapter.data.addAll(it.data.list)
                goodsSearchAdapter.notifyDataSetChanged()
                if(it.data.list.size==0){
                    ToastUtils.showLong("未找到更多数据")
                }
            }
        }
    }

}